package cc.bluemc.modules.generateCode.action;

import cc.bluemc.modules.generateCode.entity.GeneratorBean;
import cc.bluemc.modules.generateCode.entity.TableBean;
import cc.bluemc.modules.generateCode.service.GenerateServiceFactory;
import cc.bluemc.modules.generateCode.service.IGenerateService;
import cc.bluemc.platform.comm.ActionResultMap;
import cc.bluemc.platform.comm.BaseAction;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.File;
import java.io.IOException;
import java.util.List;
@Controller
@RequestMapping("/generate")
public class GenerateAction extends BaseAction{
	@Autowired
	private IGenerateService generateService;
	
	@Value("#{config.generate_outputPath}")
    private String outputPath;
	@Value("#{config.generate_packageBase}")
    private String packageBase;
	
	
	/**
	 * 代码生成
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/",method=RequestMethod.POST)
	public ActionResultMap generate(GeneratorBean generator ,@RequestParam(value="slctTb[]",required=false)String [] slctTb){
		try {
			generator.setTempPath(getBasePath()+GeneratorBean.TEMP_BASEPATH+"/"+generator.getTempPath());
			generator.setTableNames(slctTb);
			generateService.generateCode(generator);
			resultMap.setSuccess(true);
			resultMap.setMsg("代码生成成功"+generator.getOutputPath());
			log.info("代码生成成功,输出:"+generator.getOutputPath());
		} catch (Exception e) {
			resultMap.setSuccess(false);
			resultMap.setMsg("代码生成失败"+generator.getOutputPath());
		}
		
		return resultMap;
	}
	
	/**
	 * 模板
	 * @param path
	 * @return
	 * @throws java.io.IOException
	 * @throws java.io.IOException
	 */
	@ResponseBody
	@RequestMapping("ajax/template")
	public ActionResultMap template(String tempPath,Model model) throws IOException{
		model.addAttribute(_RESULTMAP, resultMap);
		String dir= getBasePath()+GeneratorBean.TEMP_BASEPATH;
		if(StringUtils.isNotBlank(tempPath)){
			dir+="/"+tempPath;
		}
		File temp = new File(dir);
		resultMap.setSuccess(temp.exists());
		if(resultMap.isSuccess()){
			resultMap.setData(temp.listFiles());
			File readMe= new File(dir+"/readMe.txt");
			if(readMe.exists()){
				resultMap.setMsg(FileUtils.readFileToString(readMe,"UTF-8"));
			}
			model.addAttribute(_RESULTMAP, resultMap);
		}
		return resultMap;
	} 
	
	/**
	 * 查看table表详细页面，
	 * @param dbName
	 * @param tbName
	 * @param model
	 * @return
	 */
	@RequestMapping("/ajax/tableForm")
	public String tableForm(String dbName,String tbName,Model model){	
		model.addAttribute("dbName", dbName);
		if(StringUtils.isNotBlank(tbName)){
			TableBean table = generateService.getTable(dbName, tbName);
			model.addAttribute("bean", table);
		}
		return "generateCode/tableForm";
	}
	
	/**
	 * 数据表
	 * @param dbName
	 * @param model
	 * @return
	 */
	@RequestMapping("/tableList")
	@RequiresRoles("coding")
	public String tableList(String dbName,Model model){
		if(StringUtils.isNotBlank(dbName)){
			List<TableBean> list = generateService.getTableList(dbName);
			resultMap.setData(list);
			model.addAttribute(_RESULTMAP, resultMap);
			model.addAttribute("dbName", dbName);
			model.addAttribute("outputPath", outputPath);
			model.addAttribute("packageBase", packageBase);
		}
		return "generateCode/tableList";
	}

	public void setGenerateService(IGenerateService generateService) {
		this.generateService = GenerateServiceFactory.createGenerateService();
	}
	
}
